package com.hemaapp.yjnh.bean;

import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import xtom.frame.XtomObject;
import xtom.frame.exception.DataParseException;

/**帖子评论
 * 
 */
public class Reply extends XtomObject implements Serializable {

	private static final long serialVersionUID = 1L;

	private String id;// 回复主键id
	private String client_id;// 回复作者主键id 用于点击作者头像进入作者主页
	private String nickname;// 回复作者昵称
	private String avatar;// 回复作者头像
	private String content;// 回复内容
	private String regdate;// 回复时间	
	private String imgurl0;// 第一张评论的小图	
	private String imgurl0big;// 第一张评论的小图	
	private String imgurl1;// 第2张评论的小图	
	private String imgurl1big;// 第2张评论的小图	
	private String imgurl2;// 第3张评论的小图	
	private String imgurl2big;// 第3张评论的小图	
	private String imgurl3;// 第4张评论的小图	
	private String imgurl3big;// 第4张评论的小图
	private String sellerflag;// 商家回复标志	0否；1是
	private ArrayList<ReplyChild> childs = new ArrayList<ReplyChild>();
	private ArrayList<ImgItemsBean> items = new ArrayList<>();

	public Reply(JSONObject jsonObject) throws DataParseException {
		if (jsonObject != null) {
			try {
				id = get(jsonObject, "id");
				client_id = get(jsonObject, "client_id");
				nickname = get(jsonObject, "nickname");
				avatar = get(jsonObject, "avatar");
				content = get(jsonObject, "content");
				regdate = get(jsonObject, "regdate");
				sellerflag = get(jsonObject, "sellerflag");
				imgurl0 = get(jsonObject, "imgurl0");
				imgurl0big = get(jsonObject, "imgurl0big");
				imgurl1 = get(jsonObject, "imgurl1");
				imgurl1big = get(jsonObject, "imgurl1big");
				imgurl2 = get(jsonObject, "imgurl2");
				imgurl2big = get(jsonObject, "imgurl2big");
				imgurl3 = get(jsonObject, "imgurl3");
				imgurl3big = get(jsonObject, "imgurl3big");
				if (!jsonObject.isNull("reply_from_data")
						&& !isNull(jsonObject.getString("reply_from_data"))) {
					JSONArray gObj=jsonObject.getJSONArray("reply_from_data");
					for(int i=0;i<gObj.length();i++){
						childs.add(new ReplyChild((JSONObject) gObj.get(i)));
					}
				}
				
				if (!jsonObject.isNull("imgItems")
						&& !isNull(jsonObject.getString("imgItems"))) {
					JSONArray jsons = jsonObject.getJSONArray("imgItems");
					for (int i = 0; i < jsons.length(); i++) {
	                    JSONObject child = jsons.getJSONObject(i);
	                    items.add(new Gson().fromJson(child.toString(), ImgItemsBean.class));
	                }
				}
			} catch (JSONException e) {
				throw new DataParseException(e);
			}
		}
	}

	@Override
	public String toString() {
		return "Reply [id=" + id + ", client_id=" + client_id + ", nickname="
				+ nickname + ", avatar=" + avatar + ", content=" + content
				+ ", regdate=" + regdate + ", imgurl0=" + imgurl0 + ", imgurl0big=" + imgurl0big + 
				", imgurl1=" + imgurl1 + ", imgurl1big=" + imgurl1big + 
				", imgurl2=" + imgurl2 + ", imgurl2big=" + imgurl2big + ", imgurl3=" + imgurl3 + 
				", imgurl3big=" + imgurl3big + "]";
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @return the client_id
	 */
	public String getClient_id() {
		return client_id;
	}

	/**
	 * @return the nickname
	 */
	public String getNickname() {
		return nickname;
	}

	/**
	 * @return the avatar
	 */
	public String getAvatar() {
		return avatar;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @return the regdate
	 */
	public String getRegdate() {
		return regdate;
	}


	/**
	 * @return the imgurl0
	 */
	public String getImgurl0() {
		return imgurl0;
	}

	/**
	 * @return the imgurl0big
	 */
	public String getImgurl0big() {
		return imgurl0big;
	}

	/**
	 * @return the imgurl1
	 */
	public String getImgurl1() {
		return imgurl1;
	}

	/**
	 * @return the imgurl1big
	 */
	public String getImgurl1big() {
		return imgurl1big;
	}

	/**
	 * @return the imgurl2
	 */
	public String getImgurl2() {
		return imgurl2;
	}

	/**
	 * @return the imgurl2big
	 */
	public String getImgurl2big() {
		return imgurl2big;
	}

	/**
	 * @return the imgurl3
	 */
	public String getImgurl3() {
		return imgurl3;
	}

	/**
	 * @return the imgurl3big
	 */
	public String getImgurl3big() {
		return imgurl3big;
	}

	public String getSellerflag() {
		return sellerflag;
	}

	public ArrayList<ReplyChild> getChilds() {
		return childs;
	}

	public ArrayList<ImgItemsBean> getItems() {
		return items;
	}

	public class ReplyChild extends XtomObject implements Serializable {
		
		private static final long serialVersionUID = 1L;
		
//		private String id;// 主键id	
		private String regdate;// 回复日期
		private String content ;// 回复内容
		
		public ReplyChild(JSONObject jsonObject) throws DataParseException {
				if (jsonObject != null) {
					try {
//						id = get(jsonObject, "id");
						regdate  = get(jsonObject, "regdate");
						content  = get(jsonObject, "content");
						
					} catch (JSONException e) {
						throw new DataParseException(e);
					}
				}
		}

		public String getRegdate() {
			return regdate;
		}

		public String getContent() {
			return content;
		}
		
	}

}
